//
//  LGHospitalPlanDateView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/8.
//

#import "LGHospitalPlanDateView.h"
#import "DatePickerTwoView.h"
@interface LGHospitalPlanDateView()<DatePickerViewDelegate>
/**   */
@property (nonatomic , strong)UIButton *titleBtn;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UILabel *startTitle;
/**   */
@property (nonatomic , strong)UIButton *startBtn;
/**   */
@property (nonatomic , strong)UILabel *endTitle;
/**   */
@property (nonatomic , strong)UIButton *endBtn;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;

@end

@implementation LGHospitalPlanDateView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.startDate = @"";
        self.endDate = @"";
        self.backgroundColor = [UIColor whiteColor];
        self.cornerRidus = 15;
        [self addSubview:self.titleBtn];
        [self addSubview:self.lineView];
        [self addSubview:self.startTitle];
        [self addSubview:self.startBtn];
        [self addSubview:self.endTitle];
        [self addSubview:self.endBtn];
    }
    return self;
}

-(void)setStartDate:(NSString *)startDate{
    _startDate = startDate;
    NSString *colorString = (startDate.length>0)?@"#333333":@"#999999";
    NSString *content = (startDate.length>0)?startDate:@"请选择";
    [self.startBtn setTitleColor:[UIColor colorWithString:colorString] forState:UIControlStateNormal];
    [self.startBtn setTitle:content forState:UIControlStateNormal];
    [self.startBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
}

-(void)setEndDate:(NSString *)endDate{
    _endDate = endDate;
    NSString *colorString = (endDate.length>0)?@"#333333":@"#999999";
    NSString *content = (endDate.length>0)?endDate:@"请选择";
    [self.endBtn setTitleColor:[UIColor colorWithString:colorString] forState:UIControlStateNormal];
    [self.endBtn setTitle:content forState:UIControlStateNormal];
    [self.endBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
}


//选择日期
-(void)selecteDateAction:(UIButton *)sender{
    if(self.selectDateBegin){
        self.selectDateBegin();
    }
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = [sender.titleLabel.text isEqualToString:@"请选择"]?@"":sender.titleLabel.text;
    pickerView.delegate = self;
    pickerView.index = sender.tag;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    if(index == 1000){
        if([self caculateDateCorrect:date endDate:self.endDate] == YES){
            self.startDate = date;
        }else{
            [TooltipView showMessage:@"开始日期不能晚于截止日期" offset:0];
        }
    }else{
        if([self caculateDateCorrect:self.startDate endDate:date] == YES){
            self.endDate = date;
        }else{
            [TooltipView showMessage:@"截止日期不能早于开始日期" offset:0];
        }
    }
}

-(BOOL)caculateDateCorrect:(NSString *)startDte  endDate:(NSString *)endDate{
    if(startDte.length == 0) return YES;
    if(endDate.length == 0) return YES;
    NSDate *start = [self.formatter dateFromString:startDte];
    NSDate *end = [self.formatter dateFromString:endDate];
    NSComparisonResult result = [start compare:end];
    NSLog(@">>???>>>%@--%@--%d",startDte,endDate,result);
    if(result == NSOrderedAscending || result == NSOrderedSame) return YES;
    return NO;
}

#pragma mark
#pragma mark ====> 创建控件
-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}
-(UIButton *)titleBtn{
    if(!_titleBtn){
        _titleBtn = [UIButton buttonWithTitle:@" 计划入院日期" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#333333" imageName:@"计划入院-日期"];
        _titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _titleBtn.frame = CGRectMake(viewPix(12), 0, Screen_W-viewPix(60), viewPix(48));
        _titleBtn.userInteractionEnabled = NO;
    }
    return _titleBtn;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(12), viewPix(48), Screen_W-viewPix(56), 0.5)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
    }
    return _lineView;
}


-(UILabel *)startTitle{
    if(!_startTitle){
        _startTitle = [UILabel lableWithText:@"起" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _startTitle.frame = CGRectMake(viewPix(12), kMaxY(self.lineView.frame)+viewPix(6), viewPix(30), viewPix(34));
    }
    return _startTitle;
}

-(UIButton *)startBtn{
    if(!_startBtn){
        _startBtn = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(16) textColor:@"#999999" imageName:@"grayTip12"];
        _startBtn.frame = CGRectMake(viewPix(45), kMinY(self.startTitle.frame), Screen_W-viewPix(89), viewPix(34));
        _startBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_startBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        _startBtn.tag = 1000;
        [_startBtn addTarget:self action:@selector(selecteDateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startBtn;
}

-(UILabel *)endTitle{
    if(!_endTitle){
        _endTitle = [UILabel lableWithText:@"止" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _endTitle.frame = CGRectMake(viewPix(12), kMaxY(self.startTitle.frame), viewPix(30), viewPix(34));
    }
    return _endTitle;
}

-(UIButton *)endBtn{
    if(!_endBtn){
        _endBtn = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(16) textColor:@"#999999" imageName:@"grayTip12"];
        _endBtn.frame = CGRectMake(viewPix(45), kMinY(self.endTitle.frame), Screen_W-viewPix(89), viewPix(34));
        _endBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_endBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        _endBtn.tag = 2000;
        [_endBtn addTarget:self action:@selector(selecteDateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endBtn;
}

@end
